package com.thejoyrun.retrofit.http.Interceptor;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;

/**
 * Created by kkmike999 on 2016/05/08.
 *
 * http请求头部信息 拦截器
 */
public class HeaderInterceptor implements Interceptor {

	static Headers headers = getPhoneHeaders();

	@Override
	public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
		try {
			Request original = chain.request();

			Request request = original.newBuilder()
			                          .headers(headers)
			                          .method(original.method(), original.body())
			                          .build();

			return chain.proceed(request);
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException("网络连接超时");
		} catch (UnknownHostException e) {
			throw new UnknownHostException("网络未连接");
		}
	}

	/**
	 * 手机信息headers
	 *
	 * @return
	 */
	protected static Headers getPhoneHeaders() {
		Map<String, String> headers = new HashMap<>();

		headers.put("MODELTYPE", "小米4");
		headers.put("SYSVERSION", "android 6.0");
		headers.put("APPVERSION", "1840");
		headers.put("MODELIMEI", "xxxxxxx");
		headers.put("Accept-Language", "zh_CN"); // 返回中英文提示
		//			headers.put("Accept-Language", LanguageUtils.isZh() ? "zh_CN" : "en_US"); // 返回中英文提示

		return Headers.of(headers);
	}
}
